package no.ntnu.fp.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import java.awt.FlowLayout;

import javax.swing.JButton;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.ScrollPaneConstants;
import java.awt.Font;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.toedter.calendar.JDayChooser;
import com.toedter.components.JLocaleChooser;
import com.toedter.components.JSpinField;
import com.toedter.calendar.JCalendar;
import javax.swing.UIManager;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.ListSelectionModel;

import no.ntnu.fp.model.Reply;
import no.ntnu.fp.net.DBConnection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import com.michaelbaranov.microba.calendar.DatePicker;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.beans.PropertyVetoException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import com.michaelbaranov.microba.calendar.CalendarPane;
import javax.swing.JCheckBox;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;

import run.Main;

import com.michaelbaranov.microba.common.CommitListener;
import com.michaelbaranov.microba.common.CommitEvent;
import javax.swing.JList;
import javax.swing.AbstractListModel;

public class TrackMeetingRequest extends JFrame {

	private JPanel contentPane;
	private static DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK);
	private JTable tableWrapper;
	private DefaultTableModel tableModel;
	private JTable tableRight;
	private DefaultTableModel tableModelRight;
	
	/**
	 * Create the frame.
	 */
	public TrackMeetingRequest() {
		setTitle("Track Meetings");
		this.setVisible(true);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1004, 543);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(6, 54, 483, 444);
		contentPane.add(panel);
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane);
	
		// Table 1
		tableWrapper = new JTable();
		tableWrapper.setBackground(Color.WHITE);
		tableWrapper.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				if (tableWrapper.getSelectedRow() >= 0 && tableWrapper.getSelectedRow() <= tableWrapper.getRowCount()){
					getAttendants();
					
				}
				else {
					//TODO: and here <--
					System.out.println("I debug, I shouldnt print ever");
				}

			}
		});
		tableWrapper.setFillsViewportHeight(true);
		tableWrapper.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		tableWrapper.setShowVerticalLines(false);

		scrollPane.setViewportView(tableWrapper);
		tableModel = new DefaultTableModel(new Object[][]{},
				new String[] {
				"Id","Date","Start", "Finish", "Description"})
		{	
			boolean[] columnEditables = new boolean[] {
					false,false, false, false,false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		};
		tableWrapper.setModel(tableModel);
		tableWrapper.getColumnModel().getColumn(0).setPreferredWidth(1);
		tableWrapper.getColumnModel().getColumn(1).setPreferredWidth(100);
		tableWrapper.getColumnModel().getColumn(2).setPreferredWidth(76);
		tableWrapper.getColumnModel().getColumn(3).setPreferredWidth(76);
		tableWrapper.getColumnModel().getColumn(4).setPreferredWidth(250);
		
		// table 2
		JPanel rightPanel = new JPanel();
		rightPanel.setBackground(Color.WHITE);
		rightPanel.setBounds(499, 54, 453, 444);
		contentPane.add(rightPanel);
		
		JScrollPane scrollPaneRight = new JScrollPane();
		rightPanel.add(scrollPaneRight);
		
		tableRight = new JTable();
		tableRight.setRowSelectionAllowed(false);
		tableRight.setFillsViewportHeight(true);
		tableModelRight = new DefaultTableModel(new Object[][]{},
				new String[] {
				"Name","Status"})
		{	
			boolean[] columnEditables = new boolean[] {
					false,false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		};
		tableRight.setModel(tableModelRight);
		tableRight.getColumnModel().getColumn(0).setPreferredWidth(205);
		tableRight.getColumnModel().getColumn(1).setPreferredWidth(209);
		scrollPaneRight.setViewportView(tableRight);
		tableRight.setFocusable(false);
		
		JLabel lblMeetingRequests = new JLabel("Meeting requests");
		lblMeetingRequests.setFont(new Font("SansSerif", Font.BOLD, 18));
		lblMeetingRequests.setBounds(22, 21, 265, 33);
		contentPane.add(lblMeetingRequests);
		
		JLabel lblAttendants = new JLabel("Attendants");
		lblAttendants.setFont(new Font("SansSerif", Font.BOLD, 18));
		lblAttendants.setBounds(499, 21, 265, 33);
		contentPane.add(lblAttendants);
		getAppointment();
		Reply reply;
	}
	
	protected void getAttendants() {
		
		
		tableModelRight = new DefaultTableModel(new Object[][]{},
				new String[] {
				"Name","Status"})
		{	
			boolean[] columnEditables = new boolean[] {
					false,false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		};
		tableRight.setModel(tableModelRight);
		
		try {
			ResultSet appointmentRes = Main.dbConn.selectFromDatabase(
					"SELECT DISTINCT employee.name, meetingRequest.status FROM employee,meetingRequest " +
					"WHERE meetingRequest.recipientId = employee.employeeId " +
					"AND meetingRequest.meetingId = '" + tableWrapper.getValueAt(tableWrapper.getSelectedRow(), 0).toString() + "'");
			while(appointmentRes.next()) {			
				tableModelRight.addRow(new Object[] {
						appointmentRes.getString("name"),
						appointmentRes.getString("status"),
				});

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}

	/**
	 * Gets the selected date from calendarPane, 
	 * then generates and runs query that populates table with data
	 */
	private void getAppointment(){

		try {
			ResultSet appointmentRes = Main.dbConn.selectFromDatabase(
					"SELECT DISTINCT appointment.* FROM appointment,meetingRequest WHERE meetingRequest.employeeId ='"+
					Main.user.getEmployeeId()+"' AND appointment.appointmentId = meetingRequest.meetingId ORDER BY appointment.date ASC ");
			while(appointmentRes.next()) {
					
				
				tableModel.addRow(new Object[] {
						appointmentRes.getInt("appointmentId"),
						dateFormatter.format(new Date(appointmentRes.getLong("date")*1000)),
						appointmentRes.getString("starttime"),
						appointmentRes.getString("finishtime"),
						appointmentRes.getString("description"),
				});

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
